//SkyTank Arduino interface
//Author: Leo Szeto
//Abstract: Arduino <--> Netbook interface for the SkyTank
//robot, designed to drive the motors, LEDs, and servo system

#define INSTR_LEN 8

int ledPin =  13;    // LED connected to digital pin 13
int ready = 0;
int incomingCmds = 0;
char command[INSTR_LEN - 1]; //7 characters, no null terminate

void setup()
{
  Serial.begin(4800); //Actually 9600 baud  
  pinMode(ledPin, OUTPUT);
  Serial.flush();
}

void loop()
{
  //Don't read until there are 8 bytes available
  //Each command consists of 8 bytes: This should
  //be more then enough to describe any cmd  
  
  //Listen to see if anything useful has popped up on serial. 
  int buf_vals = Serial.available();
  if(buf_vals >= INSTR_LEN)
  {
    for(int i = 0; i < INSTR_LEN - 1; i++) {command[i] = Serial.read();}   //Read 4 bytes. Do not terminate with \0
    incomingCmds++;
  }
  
  if(incomingCmds > 0) //If we have a command in the pipeline, read and execute
  {
    Serial.print(command);
    if(strcmp(command, "INIT0000") == 0) //This should be it's own function
    {
      Serial.print("ARDY0000"); //Print out 4 bytes back
    }
    else
    {
      Serial.print("FAILCODE"); //If we get this, we didn't receive any known commands
    }   
    //We are now done acting on incoming commmands (IF WE SUCCEED)
    incomingCmds--;
  }
 
}
